package com.uroad.jilinpda.bleactivation;

import android.text.TextUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 将字符串转换为TLV对象
 * 
 */
public abstract class TlvUtils {

	/**
	 * 将16进制字符串转换为TLV对象列表
	 * 
	 * @param hexString
	 * @return
	 */
	public static List<Tlv> builderTlvList(String hexString) {
		List<Tlv> tlvs = new ArrayList<Tlv>();

		int position = 0;
		// while (position != StringUtils.length(hexString)) {
		while (position != hexString.length()) {
			String _hexTag = getTag(hexString, position);
			position += _hexTag.length();

			LPositon l_position = getLengthAndPosition(hexString, position);
			int _vl = l_position.get_vL();

			position = l_position.get_position();

			// String _value = StringUtils.substring(hexString, position,
			// position + _vl * 2);
			String _value = hexString.substring(position, position + _vl * 2);

			position = position + _value.length();

			tlvs.add(new Tlv(_hexTag, _vl, _value));
		}
		return tlvs;
	}

	/**
	 * 将16进制字符串转换为TLV对象MAP
	 * 
	 * @param hexString
	 * @return
	 */
	public static Map<String, Tlv> builderTlvMap(String hexString) {

		Map<String, Tlv> tlvs = new HashMap<String, Tlv>();

		int position = 0;
		while (position != hexString.length()) {
			String _hexTag = getTag(hexString, position);

			position += _hexTag.length();

			LPositon l_position = getLengthAndPosition(hexString, position);

			int _vl = l_position.get_vL();
			position = l_position.get_position();
			String _value = hexString.substring(position, position + _vl * 2);
			position = position + _value.length();

			tlvs.put(_hexTag, new Tlv(_hexTag, _vl, _value));
		}
		return tlvs;
	}

	/**
	 * 返回最后的Value的长度
	 * 
	 * @param hexString
	 * @param position
	 * @return
	 */
	private static LPositon getLengthAndPosition(String hexString, int position) {
		String firstByteString = hexString.substring(position, position + 2);
		int i = Integer.parseInt(firstByteString, 16);
		String hexLength = "";

		if (((i >>> 7) & 1) == 0) {
			hexLength = hexString.substring(position, position + 2);
			position = position + 2;
		} else {
			// 当最左侧的bit位为1的时候，取得后7bit的值，
			int _L_Len = i & 127;
			position = position + 2;
			hexLength = hexString.substring(position, position + _L_Len * 2);
			// position表示第一个字节，后面的表示有多少个字节来表示后面的Value值
			position = position + _L_Len * 2;
		}
		return new LPositon(Integer.parseInt(hexLength, 16), position);

	}

	/**
	 * 取得子域Tag标签
	 *
	 * @param hexString
	 * @param position
	 * @return
	 */
	private static String getTag(String hexString, int position) {
		// String firstByte = StringUtils.substring(hexString, position,
		// position + 2);
		String firstByte = hexString.substring(position, position + 2);
		int i = Integer.parseInt(firstByte, 16);
		if ((i & 0x1f) == 0x1f) {
			return hexString.substring(position, position + 4);

		} else {
			return hexString.substring(position, position + 2);
		}
	}
	
	/**根据判断出错时是否需要执行下一段指令
	 * @param tag
	 * @return
	 */
	public static boolean isNeedExecuteNextByTag(String tag){
		int t = Integer.parseInt(tag, 16);
		int bool = ((t & 0x40) >> 6);// 40H == 0100 0000B
		return bool==1;
	}

	public static final Tlv makeTlv(String value) {
		Tlv tlv = new Tlv("80", value.length() / 2, value);
		return tlv;
	}

	public static final String easyTlvHex(String value) {
		Tlv tlv = new Tlv("80", value.length(), value);
		String tlvHex = new String();
		tlvHex = tlv.getTag() + decToHex(tlv.getLength()) + tlv.getValue();
		return tlvHex;
	}

	
	/**
	 * 根据传入TLV格式数据，响应相应TLV tag 数据，用于返回给服务器
	 * @param resplist
	 * @param reqTlvList
	 * @return
	 */
	public static final String getRespTlvString(List<String> resplist, List<Tlv> reqTlvList) {
		String tlvSum = new String();
		for (int i = 0; i < resplist.size(); i++) {
			String tlv = resplist.get(i);
			String len = getValueLength(tlv.length() / 2);
			String tag = "";

			if (isNeedResp(reqTlvList.get(i).getTag())) {
				tag = reqTlvList.get(i).getTag();
				tlvSum = tlvSum + tag + len + tlv;
			}
		}

		String sumValue = getValueLength(tlvSum.length() / 2);
		String sumTag = "81";
		tlvSum = sumTag + sumValue + tlvSum;
		return tlvSum;
	}

	/***
	 * 是否需要发回数据给服务器
	 * 
	 * @param tag
	 * @return
	 */
	private static boolean isNeedResp(String tag) {
//		第7位为1则不需要响应数据 return false;
		if (Integer.parseInt(tag, 16) >= 128) {
			return false;
		}
		return true;
	}

	public static final String getValueLength(int decValue) {
		String len;
		if (decValue / 128 == 0) {
			len = decToHex(decValue);
		} else {
			int count = decValue / 255;
			if (decValue % 255 != 0) {
				count++;
			}
			len = "8" + count + decToHex(decValue);
		}

		return len;
	}

	public static final String decToHex(int data) {
		String hex = Integer.toHexString(data);
		if (hex.length() % 2 != 0) {
			hex = "0" + hex;
		}
		return hex.toUpperCase();
	}
	
	/**
	 * 解析tlv数据，获取待执行指令
	 * @param data
	 * @return
	 */
	public static final List<Tlv> getSendTlv(String data){
		List<Tlv> tlvList = TlvUtils.builderTlvList(data);
		List<Tlv> resultList = new ArrayList<Tlv>();
		for (Tlv tlv : tlvList) {
			if(!TextUtils.isEmpty(tlv.getValue())){
				List<Tlv> tlvList2 = TlvUtils.builderTlvList(tlv.getValue());
				for (Tlv tlv2 : tlvList2) {
					if(!TextUtils.isEmpty(tlv2.getValue())){
						resultList.add(tlv2);
					}
				}
			}
		}
		return resultList;
	}
}
